home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / 3d_lib.zip / DEMO3D.C < prev    next >
C/C++ Source or Header  |  1990-12-13  |  8KB  |  282 lines

  1. #include "3d.h"
  2. #include <alloc.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <float.h>
  6. #include <graphics.h>
  7.  
  8. void main()
  9.  
  10. /* 3D Transforms Demonstration Program.  Requires Turbo C Version 1.5.
  11.    Project file should contain the line
  12.  
  13.                 DEMO3D.C GRAPHICS.LIB 3D.LIB DRIVERS.LIB
  14.  
  15.    The library directory must include 3D.LIB and GRAPHICS.LIB, and the
  16.    include directory must include 3D.H.  DRIVERS.LIB must be created by
  17.    converting the .BGI files to .OBJ files (using the BGIOBJ utility in
  18.    Turbo C version 1.5) and collecting them in a library (using TLIB).
  19.  
  20.    The program displays a cube, a tetrahedron, and an octahedron in
  21.    three dimensions.  Each figure is rotated about a different axis. */
  22.  
  23. {
  24.     FACE *f,*g,*h,*i,*j,*k;                /* Faces of cube */
  25.     FACE *t1,*t2,*t3,*t4;                  /* Faces of tetrahedron */
  26.     FACE *o1,*o2,*o3,*o4,*o5,*o6,*o7,*o8;  /* Faces of octahedron */
  27.     VECTOR n,s;
  28.     MATRIX xm,ym,xr,yr,om,or,id;           /* Transformation matrices */
  29.     OBJECT *o;                             /* Cube */
  30.     OBJECT *p;                             /* Tetrahedron */
  31.     OBJECT *q;                             /* Octahedron */
  32.     int g_driver,g_mode;
  33.     int apage,vpage,tpage,frame;
  34.  
  35.     /* Initialize matrices and data structures */
  36.  
  37.     identity (xm);
  38.     identity (ym);
  39.     identity (xr);
  40.     identity (yr);
  41.     identity (id);
  42.     identity (om);
  43.     identity (or);
  44.  
  45.     f = (FACE *)malloc(sizeof(FACE));
  46.     g = (FACE *)malloc(sizeof(FACE));
  47.     h = (FACE *)malloc(sizeof(FACE));
  48.     i = (FACE *)malloc(sizeof(FACE));
  49.     j = (FACE *)malloc(sizeof(FACE));
  50.     k = (FACE *)malloc(sizeof(FACE));
  51.  
  52.     t1 = (FACE *)malloc(sizeof(FACE));
  53.     t2 = (FACE *)malloc(sizeof(FACE));
  54.     t3 = (FACE *)malloc(sizeof(FACE));
  55.     t4 = (FACE *)malloc(sizeof(FACE));
  56.  
  57.     o1 = (FACE *)malloc(sizeof(FACE));
  58.     o2 = (FACE *)malloc(sizeof(FACE));
  59.     o3 = (FACE *)malloc(sizeof(FACE));
  60.     o4 = (FACE *)malloc(sizeof(FACE));
  61.     o5 = (FACE *)malloc(sizeof(FACE));
  62.     o6 = (FACE *)malloc(sizeof(FACE));
  63.     o7 = (FACE *)malloc(sizeof(FACE));
  64.     o8 = (FACE *)malloc(sizeof(FACE));
  65.  
  66.     o = (OBJECT *)malloc(sizeof(OBJECT));
  67.     new_obj (o);
  68.  
  69.     p = (OBJECT *)malloc(sizeof(OBJECT));
  70.     new_obj (p);
  71.  
  72.     q = (OBJECT *)malloc(sizeof(OBJECT));
  73.     new_obj (q);
  74.  
  75.     /* Define cube */
  76.  
  77.     new_face (f);
  78.     add_corner (   0.0,   0.0,   0.0,f);
  79.     add_corner ( 100.0,   0.0,   0.0,f);
  80.     add_corner ( 100.0, 100.0,   0.0,f);
  81.     add_corner (   0.0, 100.0,   0.0,f);
  82.     add_face (o,f);
  83.  
  84.     new_face (g);
  85.     add_corner (   0.0,   0.0,   0.0,g);
  86.     add_corner (   0.0, 100.0,   0.0,g);
  87.     add_corner (   0.0, 100.0, 100.0,g);
  88.     add_corner (   0.0,   0.0, 100.0,g);
  89.     add_face (o,g);
  90.  
  91.     new_face (h);
  92.     add_corner (   0.0,   0.0,   0.0,h);
  93.     add_corner (   0.0,   0.0, 100.0,h);
  94.     add_corner ( 100.0,   0.0, 100.0,h);
  95.     add_corner ( 100.0,   0.0,   0.0,h);
  96.     add_face (o,h);
  97.  
  98.     new_face (i);
  99.     add_corner (   0.0,   0.0, 100.0,i);
  100.     add_corner (   0.0, 100.0, 100.0,i);
  101.     add_corner ( 100.0, 100.0, 100.0,i);
  102.     add_corner ( 100.0,   0.0, 100.0,i);
  103.     add_face (o,i);
  104.  
  105.     new_face (j);
  106.     add_corner (   0.0, 100.0,   0.0,j);
  107.     add_corner ( 100.0, 100.0,   0.0,j);
  108.     add_corner ( 100.0, 100.0, 100.0,j);
  109.     add_corner (   0.0, 100.0, 100.0,j);
  110.     add_face (o,j);
  111.  
  112.     new_face (k);
  113.     add_corner ( 100.0,   0.0,   0.0,k);
  114.     add_corner ( 100.0,   0.0, 100.0,k);
  115.     add_corner ( 100.0, 100.0, 100.0,k);
  116.     add_corner ( 100.0, 100.0,   0.0,k);
  117.     add_face (o,k);
  118.  
  119.     /* Define tetrahedron */
  120.  
  121.     new_face (t1);
  122.     add_corner (   0.00,   0.00,   0.00,t1);
  123.     add_corner ( 100.00,   0.00,   0.00,t1);
  124.     add_corner (  50.00,  81.65,  28.87,t1);
  125.     add_face (p,t1);
  126.  
  127.     new_face (t2);
  128.     add_corner (   0.00,   0.00,   0.00,t2);
  129.     add_corner (  50.00,   0.00,  86.60,t2);
  130.     add_corner ( 100.00,   0.00,   0.00,t2);
  131.     add_face (p,t2);
  132.  
  133.     new_face (t3);
  134.     add_corner (   0.00,   0.00,   0.00,t3);
  135.     add_corner (  50.00,  81.65,  28.87,t3);
  136.     add_corner (  50.00,   0.00,  86.60,t3);
  137.     add_face (p,t3);
  138.  
  139.     new_face (t4);
  140.     add_corner ( 100.00,   0.00,   0.00,t4);
  141.     add_corner (  50.00,   0.00,  86.60,t4);
  142.     add_corner (  50.00,  81.65,  28.87,t4);
  143.     add_face (p,t4);
  144.  
  145.     /* Define octahedron */
  146.  
  147.     new_face (o1);
  148.     add_corner (   0.00,  70.71,   0.00,o1);
  149.     add_corner (  50.00,   0.00,  50.00,o1);
  150.     add_corner ( 100.00,  70.71,   0.00,o1);
  151.     add_face (q,o1);
  152.  
  153.     new_face (o2);
  154.     add_corner ( 100.00,  70.71,   0.00,o2);
  155.     add_corner (  50.00,   0.00,  50.00,o2);
  156.     add_corner ( 100.00,  70.71, 100.00,o2);
  157.     add_face (q,o2);
  158.  
  159.     new_face (o3);
  160.     add_corner ( 100.00,  70.71, 100.00,o3);
  161.     add_corner (  50.00,   0.00,  50.00,o3);
  162.     add_corner (   0.00,  70.71, 100.00,o3);
  163.     add_face (q,o3);
  164.  
  165.     new_face (o4);
  166.     add_corner (   0.00,  70.71, 100.00,o4);
  167.     add_corner (  50.00,   0.00,  50.00,o4);
  168.     add_corner (   0.00,  70.71,   0.00,o4);
  169.     add_face (q,o4);
  170.  
  171.     new_face (o5);
  172.     add_corner (   0.00,  70.71,   0.00,o5);
  173.     add_corner (  50.00, 141.40,  50.00,o5);
  174.     add_corner (   0.00,  70.71, 100.00,o5);
  175.     add_face (q,o5);
  176.  
  177.     new_face (o6);
  178.     add_corner (   0.00,  70.71, 100.00,o6);
  179.     add_corner (  50.00, 141.40,  50.00,o6);
  180.     add_corner ( 100.00,  70.71, 100.00,o6);
  181.     add_face (q,o6);
  182.  
  183.     new_face (o7);
  184.     add_corner ( 100.00,  70.71, 100.00,o7);
  185.     add_corner (  50.00, 141.40,  50.00,o7);
  186.     add_corner ( 100.00,  70.71,   0.00,o7);
  187.     add_face (q,o7);
  188.  
  189.     new_face (o8);
  190.     add_corner ( 100.00,  70.71,   0.00,o8);
  191.     add_corner (  50.00, 141.40,  50.00,o8);
  192.     add_corner (   0.00,  70.71,   0.00,o8);
  193.     add_face (q,o8);
  194.  
  195.     /* The light source in the z direction (from the eye) */
  196.  
  197.     s[0] =  0.25;
  198.     s[1] =  0.0;
  199.     s[2] =  1.0;
  200.  
  201.     /* Center figures in space and orient them */
  202.  
  203.     /* The following series of function calls illustrates the concatenation
  204.        of 3D transforms.  Each of three matrices xm, ym, and om is the
  205.        concatenation of four transforms; first, center the object on the
  206.        origin, second, rotate the object about the y axis, third, rotate the
  207.        object about the x axis, and fourth, translate the object to its final
  208.        position.  All four transforms are concatenated in each matrix, then
  209.        the object is transformed.  Thus, each vertex in the object is mul-
  210.        tiplied by the transformation matrix only once.    */
  211.  
  212.     trans (-50,-50,-50,xm);
  213.     trans (-50,-50,-50,ym);
  214.     trans (-50,-50,-50,om);
  215.     yrot (M_PI/4,xm);
  216.     yrot (M_PI/3,ym);
  217.     yrot (M_PI/4,om);
  218.     xrot (M_PI/4,xm);
  219.     xrot (M_PI/3,ym);
  220.     xrot (M_PI/4,om);
  221.  
  222.     /* Position figures in space */
  223.  
  224.     trans (200,200,200,xm);
  225.     trans (400,150,200,ym);
  226.     trans (500,250,200,om);
  227.     xform (*o,xm);
  228.  
  229.     xform (*p,ym);
  230.     xform (*q,om);
  231.  
  232.     /* Set up rotation matrices */
  233.  
  234.     /* These matrices are created to perform an incremental rotation of each
  235.        object.  First, the object is translated to the origin, second, the
  236.        object is rotated, and third, the object is translated back to its
  237.        original postion. */
  238.  
  239.     trans (-200,-200,-200,xr);
  240.     yrot (M_PI/45,xr);
  241.     trans (200,200,200,xr);
  242.  
  243.     trans (-400,-150,-200,yr);
  244.     xrot (M_PI/45,yr);
  245.     trans (400,150,200,yr);
  246.  
  247.     trans (-500,-250,-200,or);
  248.     xrot (M_PI/45,or);
  249.     yrot (M_PI/45,or);
  250.     trans (500,250,200,or);
  251.  
  252.     apage = 0;
  253.     vpage = 1;
  254.  
  255.     registerbgidriver(CGA_driver);
  256.     registerbgidriver(EGAVGA_driver);
  257.     detectgraph (&g_driver,&g_mode);
  258.     if (g_mode == VGAHI)
  259.       g_mode = EGAHI; /* VGAHI doesn't support screen flipping */
  260.     initgraph (&g_driver,&g_mode,"");
  261.  
  262.     for (frame = 1; frame <= 45; frame++)
  263.     {
  264.         setvisualpage(vpage);
  265.         setactivepage(apage);
  266.         tpage = apage;
  267.         apage = vpage;
  268.         vpage = tpage;
  269.         cleardevice();
  270.         xform (*o,xr);
  271.         xform (*p,yr);
  272.         xform (*q,or);
  273.  
  274.         disp_object (s,1,o,id);
  275.         disp_object (s,4,p,id);
  276.         disp_object (s,2,q,id);
  277.     }
  278.  
  279. closegraph();
  280.  
  281. }
  282.